package com.zszq.ms.bigmodeltest.utils;

import cn.hutool.core.date.DateUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.utils.IOUtils;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.UUID;

@Component
public class AlyOSSUtil {

    private static String endpoint;
    private static String accesskeyid;
    private static String accesskeysecret;
    private static String bucketname;

    private static String appName;

    @Value("${aliyun.oss.endpoint}")
    public void setEndpoint(String endpoint) {
        AlyOSSUtil.endpoint = endpoint;
    }

    @Value("${aliyun.oss.accesskeyid}")
    public void setAccesskeyid(String accesskeyid) {
        AlyOSSUtil.accesskeyid = accesskeyid;
    }

    @Value("${aliyun.oss.accesskeysecret}")
    public void setAccesskeysecret(String accesskeysecret) {
        AlyOSSUtil.accesskeysecret = accesskeysecret;
    }

    @Value("${aliyun.oss.bucketname}")
    public void setBucketname(String bucketname) {
        AlyOSSUtil.bucketname = bucketname;
    }

    @Value("${spring.application.name}")
    public void setAppName(String appName) {
        AlyOSSUtil.appName = appName;
    }

    /**
     * 文件上传
     *
     * @param filecontent 文件正文的byte
     * @return
     */
    public static String uploadFile(byte[] filecontent) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accesskeyid, accesskeysecret);
        String url = DateUtil.format(new Date(), "yyyy/MM/dd");
        String key = appName + "/" + url + "/" + UUID.randomUUID();
        PutObjectResult result = ossClient.putObject(bucketname, key, new ByteArrayInputStream(filecontent));
        ossClient.shutdown();
        if (StringUtils.isNotBlank(result.getETag())) {
            return key;
        }
        return "";
    }

    /**
     * 文件上传 文件流
     *
     * @param stream 文件流
     * @return 文件key
     */
    public static String uploadFile(InputStream stream) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accesskeyid, accesskeysecret);
        String url = DateUtil.format(new Date(), "yyyy/MM/dd");
        String key = appName + "/" + url + "/" + UUID.randomUUID();
        PutObjectResult result = ossClient.putObject(bucketname, key, stream);
        ossClient.shutdown();
        if (StringUtils.isNotBlank(result.getETag())) {
            return key;
        }
        return "";
    }

    /**
     * 文件下载
     *
     * @param key 文件存储key
     * @return 文件字节数组
     */
    public static byte[] downloadFile(String key) {
        byte[] bytes = null;
        OSS ossClient = new OSSClientBuilder().build(endpoint, accesskeyid, accesskeysecret);
        OSSObject object = ossClient.getObject(bucketname, key);
        try {
            bytes = IOUtils.readStreamAsByteArray(object.getObjectContent());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            ossClient.shutdown();
        }
        return bytes;
    }

    /**
     * 文件下载
     *
     * @param key 文件存储key
     * @return 文件流
     */
    public static InputStream downloadFileStream(String key) {
        byte[] bytes = null;
        OSS ossClient = new OSSClientBuilder().build(endpoint, accesskeyid, accesskeysecret);
        OSSObject object = ossClient.getObject(bucketname, key);
        return object.getObjectContent();
    }

    /**
     * 删除文件
     *
     * @param key 文件key
     */
    public static void removeFile(String key) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accesskeyid, accesskeysecret);
        ossClient.deleteObject(bucketname, key);
    }

    /**
     * 获取文件url
     *
     * @param key      文件key
     * @param expires  有效时间
     * @param calender 时间单位
     * @return 可以直接访问的文件地址
     */
    public static String getFileUrl(String key, Integer expires, Integer calender) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accesskeyid, accesskeysecret);
        try {
            Date date = new Date();
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calender, expires);
            URL url = ossClient.generatePresignedUrl(bucketname, key, calendar.getTime());
            return url.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
